﻿using Himp.Base;
using Himp.ShareData;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.BlobStoring.Database.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.OpenIddict.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;

namespace Himp.Platform.EntityFrameworkCore;

[ReplaceDbContext(typeof(IIdentityDbContext))]
[ReplaceDbContext(typeof(ITenantManagementDbContext))]
[ConnectionStringName("Default")]
public class PlatformDbContext :
    AbpDbContext<PlatformDbContext>,
    IIdentityDbContext,
    ITenantManagementDbContext
{
    /* Add DbSet properties for your Aggregate Roots / Entities here. */

    #region Entities from the modules

    /* Notice: We only implemented IIdentityDbContext and ITenantManagementDbContext
     * and replaced them for this DbContext. This allows you to perform JOIN
     * queries for the entities of these modules over the repositories easily. You
     * typically don't need that for other modules. But, if you need, you can
     * implement the DbContext interface of the needed module and use ReplaceDbContext
     * attribute just like IIdentityDbContext and ITenantManagementDbContext.
     *
     * More info: Replacing a DbContext of a module ensures that the related module
     * uses this DbContext on runtime. Otherwise, it will use its own DbContext class.
     */

    // Identity
    public DbSet<IdentityUser> Users { get; set; }
    public DbSet<IdentityRole> Roles { get; set; }
    public DbSet<IdentityClaimType> ClaimTypes { get; set; }
    public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
    public DbSet<IdentitySecurityLog> SecurityLogs { get; set; }
    public DbSet<IdentityLinkUser> LinkUsers { get; set; }
    public DbSet<IdentityUserDelegation> UserDelegations { get; set; }

    // Tenant Management
    public DbSet<Tenant> Tenants { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public DbSet<BizSeq> BizSeqs { get; set; }

    /// <summary>
    /// 机构信息
    /// </summary>
    public DbSet<Organ> Organs { get; set; }

    public DbSet<OrganGrant> OrganGrants { get; set; }

    /// <summary>
    /// 科室对应属性
    /// </summary>
    public DbSet<DeptAssocAttr> DeptAssocAttrs { get; set; }

    #region 数据字典

    /// <summary>
    /// 数据字典
    /// </summary>
    public DbSet<DataDict> DataDicts { get; set; }

    /// <summary>
    /// 数据分类
    /// </summary>
    public DbSet<CatgeDict> CatgeDicts { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public DbSet<BizDict> BizDicts { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public DbSet<AttrDict> AttrDicts { get; set; }

    /// <summary>
    /// 编码字典
    /// </summary>
    public DbSet<CodgDict> CodgDicts { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public DbSet<CodgRule> CodgRules { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public DbSet<CodgHistory> CodgHistorys { get; set; }

    #endregion

    #region 菜单

    /// <summary>
    /// 系统菜单
    /// </summary>
    public DbSet<Menu> Menus { get; set; }

    /// <summary>
    /// 角色授权
    /// </summary>
    public DbSet<MenuGrant> MenuGrants { get; set; }

    #endregion

    #region 部门

    public DbSet<Dept> Depts { get; set; }

    public DbSet<DeptGrant> DeptGrants { get; set; }

    #endregion

    /// <summary>
    /// 消息发送记录
    /// </summary>
    public DbSet<EventMsgSendRec> MsgSendRecs { get; set; }

    #region low-code

    public DbSet<AppModule> AppModules { get; set; }

    public DbSet<AppTopic> AppTopics { get; set; }

    public DbSet<DataModelDict> DataModelDicts { get; set; }

    public DbSet<DataModelExtPropertyDict> DataModelExtPropertyDicts { get; set; }

    public DbSet<DataModelPropertyDict> DataModelPropertyDicts { get; set; }

    public DbSet<EventModelDict> EventModelDicts { get; set; }

    public DbSet<EventModelPropertyDict> EventModelPropertyDicts { get; set; }

    public DbSet<StdDomainDict> StdDomainDicts { get; set; }

    public DbSet<StdWordDict> StdWordDicts { get; set; }

    public DbSet<StdPhrDict> StdPhrDicts { get; set; }

    public DbSet<DataFormDict> DataFormDicts { get; set; }

    public DbSet<DataTableDict> DataTableDicts { get; set; }

    public DbSet<DataTableColumn> DataTableColumns { get; set; }

    public DbSet<DataTableRestrRule> DataTableRestrRules { get; set; }

    public DbSet<DataViewContainerDict> DataViewContainerDicts { get; set; }

    public DbSet<DataViewDict> DataViewDicts { get; set; }

    public DbSet<DataViewValueDict> DataViewValueDicts { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public DbSet<OpenApiFuncConfig> OpenApiFuncConfigs { get; set; }

    #region 工作流

    public DbSet<AppBizflow> AppBizflows { get; set; }

    public DbSet<AppBiznode> AppBiznodes { get; set; }


    #endregion

    #endregion

    #region 患者

    /// <summary>
    /// 患者主索引
    /// </summary>
    public DbSet<PatnMasterIdx> PatnMasterIdxs { get; set; }

    /// <summary>
    /// 患者基本信息
    /// </summary>
    public DbSet<PatnBaseInfo> PatnBaseInfoes { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public DbSet<PatnCardInfo> PatnCardInfoes { get; set; }

    #endregion

    #region 疾病诊断

    public DbSet<ClncDiagDict> ClncDiagDicts { get; set; }

    public DbSet<TcmDiseDict> TcmDiseDicts { get; set; }

    public DbSet<TcmsympDict> TcmsympDicts { get; set; }

    #endregion

    #region 公共项目

    public DbSet<PubItem> PubItems { get; set; }

    public DbSet<AgmtItem> AgmtItems { get; set; }

    public DbSet<PubItemAssocChrgitmType> PubItemAssocChrgitmTypes { get; set; }

    public DbSet<PubItemBilgGrant> PubItemBilgGrants { get; set; }

    public DbSet<PubItemExecGrant> PubItemExecGrants { get; set; }

    #endregion

    #endregion

    public PlatformDbContext(DbContextOptions<PlatformDbContext> options)
        : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.ConfigurePermissionManagement();
        builder.ConfigureSettingManagement();
        builder.ConfigureBackgroundJobs();
        builder.ConfigureAuditLogging();
        builder.ConfigureIdentity();
        builder.ConfigureOpenIddict();
        builder.ConfigureFeatureManagement();
        builder.ConfigureTenantManagement();
        builder.ConfigureMainManagement();
        builder.ConfigureBlobStoring();

        //builder.Entity<YourEntity>(b =>
        //{
        //    b.ToTable(PlatformConsts.DbTablePrefix + "YourEntities", PlatformConsts.DbSchema);
        //    b.ConfigureByConvention(); //auto configure for the base class props
        //    //...
        //});
    }
}
